package org.elzat.healthboxgatewayapp.config;

import io.jsonwebtoken.security.Keys;
import lombok.Data;
import lombok.ToString;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.crypto.SecretKey;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Date;

@ConfigurationProperties(prefix = "health-box.jwt")
@Data
@RefreshScope
@ToString
public class HealthBoxJwtConfig {

    private String secretKey;
    private String tokenPrefix;
    private String tokenExpirationUnit;
    private Integer tokenExpirationValue;
    public static final String AUTHORITIES = "authorities";
    public Date getExpirationDate() {
        LocalDate now = LocalDate.now();
        switch (tokenExpirationUnit) {
            case "week":
                now = now.plusWeeks(tokenExpirationValue);
                break;
            case "day":
                now = now.plusDays(tokenExpirationValue);
                break;
            case "minute":
                now = now.plus(tokenExpirationValue, ChronoUnit.MINUTES);
                break;
        }
        return java.sql.Date.valueOf(now);
    }
    @Bean
    public SecretKey secretKey() {
        return Keys.hmacShaKeyFor(getSecretKey().getBytes(StandardCharsets.UTF_8));
    }
}
